import 'dart:convert';

import 'package:flutter_base_app/constant/sp_key.dart';
import 'package:flutter_base_app/router/routes_manager.dart';
import 'package:flutter_base_app/utils/user/user_data.dart';

import 'package:flutter_live_data/live_data_export.dart';
import 'package:flutter_common_utils/common_utils_export.dart';

import '../../api/api_main.dart';
import '../../constant/event_bus_key.dart';
import '../http/http_ui_handle.dart';

class UserHelp {
  UserHelp._();

  static UserData? _userData;
  static UserInfoData? _userInfoData;
  static final userInfoDataLiveData = LiveData<UserInfoData?>();
  static final userDataLiveData = LiveData<UserData?>();
  static bool isInit = false;
  static bool isInitUserInfoData = false;

  static UserData? get userData {
    if (!isInit) {
      isInit = true;
      final json = SpKey.userData.getStore();
      if (!json.isNullOrEmpty()) {
        _userData = UserData.fromJson(jsonDecode(json));
        userDataLiveData.value = _userData;
      }
    }
    return _userData;
  }

  static UserInfoData? get userInfoData {
    if (!isInitUserInfoData) {
      isInitUserInfoData = true;
      final json = SpKey.userInfoData.getStore();
      if (!json.isNullOrEmpty()) {
        _userInfoData = UserInfoData.fromJson(jsonDecode(json));
        userInfoDataLiveData.value = _userInfoData;
      }
    }
    return _userInfoData;
  }

  static bool setUserData(UserData? value) {
    _userData = value;
    if (value != null) {
      return SpKey.userData.putStore(jsonEncode(value.toJson()));
    }
    userDataLiveData.value = value;
    return true;
  }

  static bool setUserInfoData(UserInfoData? value) {
    _userInfoData = value;
    if (value != null) {
      return SpKey.userInfoData.putStore(jsonEncode(value.toJson()));
    }
    userInfoDataLiveData.value = value;
    return true;
  }

  static void cleanData() {
    SpKey.userData.storeRemove();
    SpKey.userInfoData.storeRemove();
    _userData = null;
    _userInfoData = null;
    userInfoDataLiveData.value = null;
    userDataLiveData.value = null;
  }

  static bool get isLogin => userData != null && userData!.token.isNotEmpty;

  ///设置登录信息
  static void setLoginInfo(String userName, String password) {
    SpKey.userName.putStore(userName);
    SpKey.password.putStore(password);
  }

  ///删除登录信息
  static void removeLoginInfo() {
    SpKey.userName.storeRemove();
    SpKey.password.storeRemove();
  }

  getLoginUserName() => SpKey.userName.getStore(def: "");

  static getLoginPassword() => SpKey.password.getStore(def: "");

  static String getUserId() => userData?.userId ?? "";

  static void logout() {
    cleanData();
    EventBusKey.EXIT_LOGIN.busPost("");
    RouterJump.login();
  }

  static void login() {
    RouterJump.login();
  }

  static Future<UserInfoData?> loadUserInfo({HttpUiHandler? handler}) async {
    var data = await ApiMain.userInfo(handler ?? (HttpUiHandler.get()..setNoTips()));
    if (data.isSucceedData) {
      UserHelp.setUserInfoData(data.dataX);
    }
    return data.data;
  }
}
